package com.bjgov.scoresys.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;

import com.bjgov.scoresys.entity.ProjectItems;
import com.bjgov.scoresys.entity.ScoreItem;
import com.bjgov.scoresys.util.Page;

/**
 * A data access object (DAO) providing persistence and search support for
 * ScoreItem entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .ScoreItem
 * @author MyEclipse Persistence Tools
 */

public class ScoreItemDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory
			.getLog(ScoreItemDAO.class);
	// property constants
	public static final String ITEM_NAME = "itemName";
	public static final String ITEM_DESC = "itemDesc";
	public static final String STATUS = "status";
	//public static final String Quality_Type = "qualityType";

	public void save(ScoreItem transientInstance) {
		log.debug("saving ScoreItem instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ScoreItem persistentInstance) {
		log.debug("deleting ScoreItem instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ScoreItem findById(java.lang.Integer id) {
		log.debug("getting ScoreItem instance with id: " + id);
		try {
			ScoreItem instance = (ScoreItem) getSession().get("com.bjgov.scoresys.entity.ScoreItem", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List<ScoreItem> findByIds(String ids) {
		log.debug("getting ScoreItem instance with ids: " + ids);
		try {
			String queryString = "from ScoreItem as model where model.id in(" + ids + ")";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("get ScoreItem with ids: " + ids + " failed", re);
			throw re;
		}
	}	

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ScoreItem instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ScoreItem as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<ScoreItem> findByItemName(Object itemName) {
		return findByProperty(ITEM_NAME, itemName);
	}

	public List<ScoreItem> findByItemDesc(Object itemDesc) {
		return findByProperty(ITEM_DESC, itemDesc);
	}

	public List<ScoreItem> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all ScoreItem instances");
		try {
			String queryString = "from ScoreItem";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ScoreItem merge(ScoreItem detachedInstance) {
		log.debug("merging ScoreItem instance");
		try {
			ScoreItem result = (ScoreItem) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ScoreItem instance) {
		log.debug("attaching dirty ScoreItem instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ScoreItem instance) {
		log.debug("attaching clean ScoreItem instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	@Override
	public int count() {
		log.debug("getScoreItemCount");
		try {
			String queryString = "select count(id) from ScoreItem";
			Query query = getSession().createQuery(queryString);
	        return (Integer) query.iterate().next();
		} catch (RuntimeException re) {
			log.error("getScoreItemCount failed", re);
			throw re;
		}
	}

	@Override
	public List getByPage(Page page) throws HibernateException {
		String querySentence = "from ScoreItem";
		Query query = getSession().createQuery(querySentence);
		// 实现分页功能
		query.setFirstResult(page.getBeginIndex())
		.setMaxResults(page.getEveryPage());
		return query.list();
	}	
}